/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication9;
import org.jgap.Chromosome;
import org.jgap.Configuration;
import org.jgap.FitnessFunction;
import org.jgap.Gene;
import org.jgap.Genotype;
import org.jgap.IChromosome;
import org.jgap.impl.DefaultConfiguration;
import org.jgap.impl.StringGene;

/**
 *
 * @author claudio
 */
public class calculoMejorRuta {
    int destino_x = 10;
    int destino_y = 14;
    int origen_x = 0;
    int origen_y = 0;
    final String Alfabeto = "ABCD";
    Object matriz;

    public calculoMejorRuta(Object matriz) {
        
    }

    public void calcularRuta(int origenX, int origenY, int destinoX, int destinoY) throws Exception {
        destino_x = destinoX;
        destino_y = destinoY;
        origen_x = origenX;
        origen_y = origenY;

        Configuration parametros = new DefaultConfiguration();
        FitnessFunction fitness = new fittnessRuta( origen_x, origen_y, destino_x, destino_y );

        parametros.setFitnessFunction( fitness );

        Gene genes = new StringGene(parametros, 1, 1, Alfabeto);
        
        IChromosome sampleChromosome = new Chromosome(parametros, genes, Math.abs(destino_x - origen_x)+ Math.abs(destino_y - origen_y));

        parametros.setSampleChromosome( sampleChromosome );
        parametros.setPopulationSize( 10000 );
        Genotype population = Genotype.randomInitialGenotype( parametros );

        int generacion = 0;
        IChromosome solucion = null;
        IChromosome rutaAlternativa = null;
        do {
            population.evolve();
            rutaAlternativa = solucion;
            solucion = population.getFittestChromosome();
            //System.out.println("Fittness: " + solucion.getFitnessValue());
            generacion++;
        } while (generacion < 20);

        String rutaCadena = fittnessRuta.getCadena(solucion);
        System.out.println("Ruta encontrada: " + rutaCadena);
        System.out.println("Fittness: " + solucion.getFitnessValue());
        getPenalizacionRuta(rutaCadena);
        //IChromosome solution = population.getFittestChromosome();
    }

    public static void getPenalizacionRuta(String ruta) {
        int suma_x=0;
        int suma_y=0;
        int tamano = ruta.length();
        for(int i = 0; i < tamano; i++) {
            char paso = ruta.charAt(i);
            switch (paso) {
                case 'A':
                    suma_x++;
                    break;
                case 'B':
                    suma_y++;
                    break;
                case 'C':
                    suma_x--;
                    break;
                case 'D':
                    suma_y--;
                    break;
            }

        }
        System.out.println("Suma X:" + suma_x);
            System.out.println("Suma Y:" + suma_y);
    }

}
